#ifndef __PORTNAMES_H__
#define __PORTNAMES_H__

#include "ch32v_def.h"

#ifdef __cplusplus
extern "C"
{
#endif

        extern GPIO_TypeDef *GPIOPort[];

        typedef enum
        {
                FirstPort = 0x00,
                PortA = FirstPort,
                PortB,
#ifdef GPIOC_BASE
                PortC,
#endif
#ifdef GPIOD_BASE
                PortD,
#endif
#ifdef GPIOE_BASE
                PortE,
#endif
#ifdef GPIOF_BASE
                PortF,
#endif
#ifdef GPIOG_BASE
                PortG,
#endif
#ifdef GPIOH_BASE
                PortH,
#endif
#ifdef GPIOI_BASE
                PortI,
#endif
#ifdef GPIOJ_BASE
                PortJ,
#endif
#ifdef GPIOK_BASE
                PortK,
#endif
#ifdef GPIOZ_BASE
                PortZ,
#endif
                PortEND,
                LastPort = PortEND - 1
        } PortName;

        #define MAX_NB_PORT (LastPort - FirstPort + 1)

        #define CH_PORT(X) (((uint32_t)(X) >> 4) & 0xF)
        #define CH_PIN(X) ((uint32_t)(X)&0xF)
        #define CH_GPIO_PIN(X) ((uint16_t)(1 << CH_PIN(X)))

        /* Return GPIO base address */
        #define get_GPIO_Port(p) ((p < MAX_NB_PORT) ? GPIOPort[p] : (GPIO_TypeDef *)NULL)
        /* Enable GPIO clock and return GPIO base address */
        GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx);

        #define REF_GPIO_PORT(X) (get_GPIO_Port(CH_PORT(X)))
        #define REF_GPIO_PIN(X) (get_GPIO_Pin(CH_PIN(X)))

#ifdef __cplusplus
}
#endif

#endif // __PORTNAMES_H__